﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AjaxPro;

using CMS.Core.DataInterfaces;
using CMS.Data;
using CMS.Core.Domain;
using CMS.Presenters.Presenters;
using CMS.Presenters.Views;
using CMS.Data.Common;

namespace CMS.Web.CompDenu
{
    public partial class AddDonThu : BaseUserControl, IAddDonThuView
    {
        private AddDonThuPresenter presenter;
        // For Nguoi su dung
        IList<LoaiDon> ldList;
        IList<LinhVuc> lvList;
        IList<NguonTiepDon> ntdList;
        IList<DiaBan> dbList;
        IList<NguoiSuDung> nsdList;
        NguoiSuDungInfo userInfo = null;
        String donthuID = "";
        DonThu _donThu = null;
        NguoiGuiDon _nguoiGuiDon = null;
        // statDone: -1 - Chưa thực hiện; 1 - Thành công; 0 - Lỗi
        public int statDone = -1;

        public AddDonThu()
        {
            this.presenter = new AddDonThuPresenter(this, NHibernateDaoFactory.Instance.GetDonThuDao());
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            userInfo = (NguoiSuDungInfo)Session["UserInfo"];
            donthuID = Request.QueryString.Get("objID")==null?"" : Request.QueryString.Get("objID");
            statDone = Request.QueryString.Get("statDone") == null ? -1 : Convert.ToInt32(Request.QueryString.Get("statDone"));
            
            this.RaiseClientEvent();
            if (!donthuID.Equals(""))
            {
                this.lblMess.InnerHtml = "";
                _donThu = DonThuDao.Instance.GetById(donthuID);
                //if (_donThu.ChuyenTieps.Count > 0)
                //{
                //    _chuyenTiep = _donThu.ChuyenTieps[0];
                //}
                //else
                //{
                //    _chuyenTiep = new ChuyenTiep();
                //    _chuyenTiep.MaDonThu = _donThu.ID;
                //}
                // Get nguoi gui don
                _nguoiGuiDon = _donThu.NguoiGuiLookup;
            }
            if (!this.IsPostBack)
            {
                if (donthuID.Equals(""))
                {
                    this.InitForm(); 
                }
                else
                {
                    this.lblTitle.InnerHtml = "HIỆU CHỈNH ĐƠN THƯ";
                    this.presenter.LoadViewFromModel();
                    InitFormEdit(_donThu);
                }
                if (statDone != -1)
                {
                    rowMess.Visible = true;
                    this.lblMess.Visible = true;
                    if (statDone == 1)
                    {
                        this.lblMess.InnerHtml = "Cập nhật thành công!";
                    }
                    else if (statDone == 0)
                    {
                        this.lblMess.InnerHtml = "Lỗi cập nhật dữ liệu!";
                    }
                }
            }
        }

        // For Ajax to validdate
        protected void RaiseClientEvent()
        {
            rowMess.Visible = false;
            Utility.RegisterTypeForAjax(typeof(CMS.Presenters.Presenters.AddDonThuPresenter));
        }

        public string Message
        {
            set { //lblMessage.Text = value; 
            }
        }

        public DonThu DonThuToEdit
        {
            get
            {
                return _donThu;
            }
            set
            {
                this._donThu = value;
            }
        }

        public DonThu DonThuToAdd
        {
            get
            {   
                DonThu dtObj;

                if (DonThuToEdit != null)
                {
                    dtObj = DonThuToEdit;
                }
                else
                {
                    dtObj = new DonThu();
                    if (hidNguoiGui.Value != "0")
                        dtObj.MaNguoiGui = Convert.ToInt32(hidNguoiGui.Value);
                }
                string[] temp = null;
                if (!txtSoThuLy.Text.Equals(""))
                    dtObj.SoThuLy = txtSoThuLy.Text.Trim();// Convert.ToInt32(txtSoThuLy.Text.Trim());
                else
                    dtObj.SoThuLy = null;
                try
                {
                    if (txtDonDeNgay.Text.Trim().Equals(""))
                    {
                        dtObj.DonDeNgay = null;
                    }
                    else
                    {
                        temp = txtDonDeNgay.Text.Trim().Split(new Char[] { '/' });
                        dtObj.DonDeNgay = new DateTime(Convert.ToInt16(temp[2]), Convert.ToInt16(temp[1]), Convert.ToInt16(temp[0]), 0, 0, 0, 0);
                    }
                }
                catch { dtObj.DonDeNgay = null; }                
                try
                {
                    
                        temp = txtNgayGui.Text.Trim().Split(new Char[] { '/' });
                        dtObj.NgayGui = new DateTime(Convert.ToInt16(temp[2]), Convert.ToInt16(temp[1]), Convert.ToInt16(temp[0]), 0, 0, 0, 0);
                    
                }
                catch { }
                
                dtObj.LanGui = Convert.ToByte(txtLanGui.Text.Trim());
                dtObj.MaDiaBan = Convert.ToInt32(hidDiaBan.Value);
                dtObj.MaLoaiDon = Convert.ToInt32(drpLoaiDon.SelectedValue);
                dtObj.MaLinhVuc = Convert.ToInt32(drpLinhVuc.SelectedValue);
                dtObj.NoiDung = txtTomTat.Value.Trim();
                if (drpLoaiHinh.SelectedValue.Equals(""))
                    dtObj.LoaiHinh = null;
                else
                    dtObj.LoaiHinh = Convert.ToInt32(drpLoaiHinh.SelectedValue);

                dtObj.MaNguonTiepDon = Convert.ToInt32(drpNguonTiepDon.SelectedValue);                
                dtObj.NguoiNhanDon = txtNguoiNhanDon.Text.Trim();
               
                dtObj.NoiDen = txtNoiDen.Text.Trim();
                dtObj.DSTapTheDeDon = txtDSTapTheDeDon.Text.Trim();
                return dtObj;
            }
            set
            {
                if (value == null) throw new ArgumentNullException("ObjectToUpdate may not be null");
            }
        }

        public NguoiGuiDon NguoiGuiToEdit
        {
            get
            {
                return _nguoiGuiDon;
            }
            set
            {
                this._nguoiGuiDon = value;
            }
        }

        public NguoiGuiDon NguoiGuiToAdd
        {
            get
            {
                NguoiGuiDon dtObj = null;

                if (NguoiGuiToEdit != null)
                {
                    dtObj = NguoiGuiToEdit;
                }
                else
                {
                    dtObj = new NguoiGuiDon();
                }
                if (hidNguoiGui.Value.Equals("0"))
                {
                    dtObj.HoTen = txtHoTen.Text.Trim();
                    dtObj.DiaChi = txtDiaChi.Text.Trim();
                    //dtObj.MaDonVi = Int32.Parse(hidDonVi.Value);
                    //dtObj.TenDonVi = drpDonVi.Text.Trim();
                }
                return dtObj;
            }
        }
        
        // For Dia ban
        public IList<DiaBan> DiaBanList
        {
            get { return this.dbList; }
            set { this.dbList = value; }
        }
        // For Loai don
        public IList<LoaiDon> LoaiDonList
        {
            get { return this.ldList; }
            set { this.ldList = value; }
        }
        // For Linh vuc
        public IList<LinhVuc> LinhVucList
        {
            get { return this.lvList; }
            set { this.lvList = value; }
        }
        // For NguonTiepDon
        public IList<NguonTiepDon> NguonTiepDonList
        {
            get { return this.ntdList; }
            set { this.ntdList = value; }
        }
        // For Nguoi su dung
        public IList<NguoiSuDung> NguoiSuDungList
        {
            get { return this.nsdList; }
            set { this.nsdList = value; }
        }
        // For Nguoi su dung
        public string getTenNguoiSuDung(string code)
        {
            return presenter.getNguoiSuDung(code).TenNguoiSuDung;
        }

        //public String VanbanCD
        //{
        //    get { return this.hidVBCD.Value; }
        //    set { this.hidVBCD.Value = value; }
        //}

        //public IList<VanBan> VBCDs
        //{
        //    set
        //    {
        //        this.drpVBCD.DataSource = value;
        //        this.drpVBCD.DataBind();
        //    }
        //}

        public String ID
        {
            get { return this.donthuID; }
            set { this.ID = value; }
        }

        public int LoaiPhieu
        {
            get { return 0;// drpLoaiPhieu.SelectedIndex; 
            }
        }
        /// <summary>
        /// Save button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnOK_Click(object sender, EventArgs e)
        {
            statDone = (presenter.AddObj(Page.IsValid)) ? 1 : 0;

            if (presenter.GetDonThuID().Equals(""))
            {
                Response.Redirect(BaseUrl + "&action=EDIT&statDone=" + Convert.ToString(statDone) + "&objID=" + ID);
            }
            else
            {
                Response.Redirect(BaseUrl + "&action=EDIT&statDone=" + Convert.ToString(statDone) + "&objID=" + presenter.GetDonThuID());
            }
        }

        protected void btnOKC_Click(object sender, EventArgs e)
        {
            presenter.AddObj(Page.IsValid);
            Response.Redirect(BaseUrl + "&action=EDIT&objID=" + ID);
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(BaseUrl + "&action=VIEW");
        }

        protected void InitForm()
        {
            txtSoThuLy.Text = Convert.ToString(presenter.GetMaxSoThuLy());
            txtNgayGui.Text = DateTime.Now.ToString("dd/MM/yyyy");            

            txtHoTen.Text = "";
            txtLanGui.Text = "1";
            txtDiaChi.Text = "";
            txtTomTat.Value = "";            
            txtNguoiNhanDon.Text = "";            
            txtNoiDen.Text = "";
            txtDSTapTheDeDon.Text = "";
            
            hidNguoiXuLy.Value = (userInfo == null) ? "" : userInfo.ID;
            hidNguoiChuyen.Value = (userInfo==null)?"":userInfo.ID;
            hidNguoiNhan.Value = (userInfo == null) ? "" : userInfo.ID;
            // For Dia ban
            this.drpDiaBan.ReadOnly = true;
            // For Loai don
            this.drpLoaiDon.DataSource = this.LoaiDonList;
            drpLoaiDon.DataTextField = "TenLoaiDon";
            drpLoaiDon.DataValueField = "ID";
            this.drpLoaiDon.DataBind();
            // For Linh vuc
            this.drpLinhVuc.DataSource = this.LinhVucList;
            drpLinhVuc.DataTextField = "TenLinhVuc";
            drpLinhVuc.DataValueField = "ID";
            this.drpLinhVuc.DataBind();
            // For NguonTiepDon
            this.drpNguonTiepDon.DataSource = this.NguonTiepDonList;
            drpNguonTiepDon.DataTextField = "TenNguonTiep";
            drpNguonTiepDon.DataValueField = "ID";
            this.drpNguonTiepDon.DataBind();                       
        }        
        
        
        protected void InitFormEdit(DonThu obj)
        {
            NguoiGuiDon objTemp = obj.NguoiGuiLookup;
            txtHoTen.Text = objTemp.HoTen;
            txtDiaChi.Text = objTemp.DiaChi;

            txtSoThuLy.Text = (obj.SoThuLy != null) ? obj.SoThuLy.ToString() : "";
            txtDonDeNgay.Text = (obj.DonDeNgay != null) ? ((DateTime)obj.DonDeNgay).ToString("dd/MM/yyyy") : "";
            txtNgayGui.Text = obj.NgayGui.ToString("dd/MM/yyyy");            

            txtLanGui.Text = obj.LanGui.ToString();
            // For Dia ban
            this.drpDiaBan.Text = obj.DiaBan.TenDiaBan;
            this.drpDiaBan.ReadOnly = true;
            this.hidDiaBan.Value = Convert.ToString(obj.MaDiaBan);
            // For Loai don
            this.drpLoaiDon.DataSource = this.LoaiDonList;
            drpLoaiDon.DataTextField = "TenLoaiDon";
            drpLoaiDon.DataValueField = "ID";
            this.drpLoaiDon.DataBind();
            ListItem item = drpLoaiDon.Items.FindByValue(obj.MaLoaiDon.ToString());
            if (item != null) item.Selected = true;
            // For Linh vuc
            this.drpLinhVuc.DataSource = this.LinhVucList;
            drpLinhVuc.DataTextField = "TenLinhVuc";
            drpLinhVuc.DataValueField = "ID";
            this.drpLinhVuc.DataBind();
            item = drpLinhVuc.Items.FindByValue(obj.MaLinhVuc.ToString());
            if (item != null) item.Selected = true;
            // For NguonTiepDon
            this.drpNguonTiepDon.DataSource = this.NguonTiepDonList;
            drpNguonTiepDon.DataTextField = "TenNguonTiep";
            drpNguonTiepDon.DataValueField = "ID";
            this.drpNguonTiepDon.DataBind();
            item = drpNguonTiepDon.Items.FindByValue(obj.MaNguonTiepDon.ToString());
            if (item != null) item.Selected = true;
            
            txtTomTat.Value = obj.NoiDung;
            txtNguoiNhanDon.Text = obj.NguoiNhanDon;
            txtNoiDen.Text = obj.NoiDen;
            txtDSTapTheDeDon.Text = obj.DSTapTheDeDon;
            //set loai hinh
            item = drpLoaiHinh.Items.FindByValue(obj.LoaiHinh.ToString());
            if (item != null) item.Selected = true;
            
            item = drpNguonTiepDon.Items.FindByValue(obj.MaNguonTiepDon.ToString());
            if (item != null) item.Selected = true;
        }        
    }
}